﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MarioArena.World;

namespace MarioArena.HUD
{
    public class RespawnDisplay
    {
        private const float transparency = 0.7f; //0 = solid, 1 = transparent
        private const int marginTop = 30; //Space between time display and top border.
        private const int marginRight = 200; //Space between left side of time display and right border.
        private const string respawnText = "Respawning in: ";
        private Color textColor = Color.Yellow;
        private Color backgroundColor = Color.Red;

        private int screenWidth;
        private int screenHeight;

        private Texture2D backgroundTexture;
        private SpriteFont font;
        private Rectangle overlayBounds;

        private bool active = false;

        private long startTime;
        private int passedTimeInSeconds;
        private int durationInSeconds;
        private string remainingTimeAsString;
        private int textPositionX;
        private int textPositionY;

        public void LoadContent()
        {
            backgroundTexture = new Texture2D(GameManager.GraphicsDevice, 1, 1);
            backgroundTexture.SetData(new[] { Color.White });
            font = GameManager.Content.Load<SpriteFont>("Fonts/Respawn");

            screenWidth = GameManager.GraphicsDevice.Viewport.Width;
            screenHeight = GameManager.GraphicsDevice.Viewport.Height;
            this.overlayBounds = new Rectangle(0, 0, screenWidth, screenHeight);
        }

        public void Update(GameTime gameTime)
        {
            if (this.active) //only update when active
            {
                int previousTimeInSeconds = this.passedTimeInSeconds;
                this.passedTimeInSeconds = (int)((DateTime.Now.Ticks - this.startTime) / 10000000f);

                //only update time if a second has passed
                if (this.passedTimeInSeconds > previousTimeInSeconds)
                {
                    this.remainingTimeAsString = (this.durationInSeconds - this.passedTimeInSeconds).ToString();
                    UpdateTextPosition();

                    //if maximum duration exceeded, close the respawn display.
                    if (passedTimeInSeconds >= this.durationInSeconds)
                        this.active = false;
                }
            }
        }

        /// <summary>
        /// Updates the position of the text in order to center it on the screen
        /// </summary>
        private void UpdateTextPosition()
        {
            string text = respawnText + this.remainingTimeAsString;
            Vector2 textSize = this.font.MeasureString(text);
            int textWidth = (int)textSize.X;
            int textHeight = (int)textSize.Y;

            this.textPositionX = (this.screenWidth - textWidth) / 2;
            this.textPositionY = (this.screenHeight - textHeight) / 2;
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            if (active) //only draw if active
            {
                //draw background
                spriteBatch.Draw(this.backgroundTexture, this.overlayBounds, this.backgroundColor * (1 - transparency));
                //draw respawn text
                spriteBatch.DrawString(font, "Respawning in: " + this.remainingTimeAsString, new Vector2(this.textPositionX, this.textPositionY), textColor);
            }
        }

        /// <summary>
        /// Activates the respawn display, showing the respawn screen with countdown for a certain amount of time.
        /// </summary>
        /// <param name="countdownInSeconds">Duration of the respawn display and countdown.</param>
        public void Activate(int countdownInSeconds)
        {  
            this.startTime = DateTime.Now.Ticks;
            this.durationInSeconds = countdownInSeconds;
            this.remainingTimeAsString = durationInSeconds.ToString();
            UpdateTextPosition();
            this.active = true;
        }
    }
}
